<?php
namespace app\api\controllers;

use app\api\models\AnalysisModel;
use Yii;
use Cm;
use app\api\core\wechat\wxAnalysis;
use app\api\core\BaseController;
use app\api\interfaces\AnalysisInterface;


class AnalysisController extends BaseController implements AnalysisInterface
{
    public function actionSync_user()
    {
        $begin_date = Cm::$req->post('begin_date', ['reg'=>'/^\d{4}-\d{2}-\d{2}/', 'title'=>'开始时间', 'null', 'empty']);
        $end_date = Cm::$req->post('end_date', ['reg'=>'/^\d{4}-\d{2}-\d{2}/', 'title'=>'结束时间', 'null', 'empty']);
        $appid = Cm::$req->post('appid', ['reg'=>'/^[0-9A-Za-z]{1,}/', 'title'=>'微信公总号APPID']);
        if (!empty($begin_date)) {
            if (strtotime($begin_date) > time()) {
                Cm::$res->error('开始时间小于当前时间');
            }

            if (!empty($end_date)) {
                if (strtotime($end_date) > time()) {
                    Cm::$res->error('结束时间小于当前时间');
                }
                $dayTime = (int)strtotime($end_date)-(int)strtotime($begin_date) ;
                if (strtotime($end_date)-strtotime($begin_date) < 0) {
                    Cm::$res->error('开始时间要小于结束时间');
                }

                if (round($dayTime/(24*3600)) > 7) {
                    Cm::$res->error('时间跨度小于等于一个星期【7天】');
                }
            } else {
                Cm::$res->error('请传入结束时间');
            }
        }
        //开始计算时间
        $wechat = new wxAnalysis();

        $result = $wechat->analysisUser($appid, $begin_date, $end_date);
        Cm::$res->susess('同步成功');
    }

    /**
     * 同步消息分析数据：默认一个月（当月）
     */
    public  function actionSync_msg()
    {
        $begin_date = Cm::$req->post('begin_date', ['reg'=>'/^\d{4}-\d{2}-\d{2}/', 'title'=>'开始时间', 'null', 'empty']);
        $end_date = Cm::$req->post('end_date', ['reg'=>'/^\d{4}-\d{2}-\d{2}/', 'title'=>'结束时间', 'null', 'empty']);
        $appid = Cm::$req->post('appid', ['reg'=>'/^[0-9A-Za-z]{1,}/', 'title'=>'微信公总号APPID']);
        if (!empty($begin_date)) {
            if (strtotime($begin_date) > time()) {
                Cm::$res->error('开始时间小于当前时间');
            }
            if (!empty($end_date)) {
                if (strtotime($end_date) > time()) {
                    Cm::$res->error('结束时间小于当前时间');
                }
                $dayTime = (int)strtotime($end_date)-(int)strtotime($begin_date) ;
                if (strtotime($end_date)-strtotime($begin_date) < 0) {
                    Cm::$res->error('开始时间要小于结束时间');
                }

                if (round($dayTime/(24*3600)) > 7) {
                    Cm::$res->error('时间跨度小于等于一个星期【7天】');
                }
            } else {
                Cm::$res->error('请传入结束时间');
            }
        }

        $wechat = new wxAnalysis();

        $result = $wechat->analysisMsg($appid, $begin_date, $end_date);
        Cm::$res->susess('同步成功！');
    }

    /**
     * 同步图文消息数据：默认一个月（当月）
     */
    public function actionSync_article()
    {
        $begin_date = Cm::$req->post('begin_date', ['reg'=>'/^\d{4}-\d{2}-\d{2}/', 'title'=>'开始时间', 'null', 'empty']);
        $end_date = Cm::$req->post('end_date', ['reg'=>'/^\d{4}-\d{2}-\d{2}/', 'title'=>'结束时间', 'null', 'empty']);
        $appid = Cm::$req->post('appid', ['reg'=>'/^[0-9A-Za-z]{1,}/', 'title'=>'微信公总号APPID']);
        if (!empty($begin_date)) {

            if (strtotime($begin_date) > time()) {
                Cm::$res->error('开始时间小于当前时间');
            }
            if (!empty($end_date)) {

                if (strtotime($end_date) > time()) {
                    Cm::$res->error('结束时间小于当前时间');
                }

                $dayTime = (int)strtotime($end_date)-(int)strtotime($begin_date) ;
                if (strtotime($end_date)-strtotime($begin_date) < 0) {
                    Cm::$res->error('开始时间要小于结束时间');
                }

                if (round($dayTime/(24*3600)) > 3) {
                    Cm::$res->error('时间跨度小于等于一个星期【3天】');
                }
            } else {
                Cm::$res->error('请传入结束时间');
            }
        }

        $wechat = new wxAnalysis();

        $wechat->analysisArticle($appid, $begin_date, $end_date);

        Cm::$res->susess('同步成功！');
    }

  public function actionSync_all()
  {
      $begin_date = Cm::$req->post('begin_date', ['reg'=>'/^\d{4}-\d{2}-\d{2}/', 'title'=>'开始时间', 'null', 'empty']);
      $end_date = Cm::$req->post('end_date', ['reg'=>'/^\d{4}-\d{2}-\d{2}/', 'title'=>'结束时间', 'null', 'empty']);
      $appid = Cm::$req->post('appid', ['reg'=>'/^[0-9A-Za-z]{1,}/', 'title'=>'微信公总号APPID']);
      if (!empty($begin_date)) {
          if (strtotime($begin_date) > time()) {
              Cm::$res->error('开始时间小于当前时间');
          }

          if (!empty($end_date)) {
              if (strtotime($end_date) > time()) {
                  Cm::$res->error('结束时间小于当前时间');
              }
              $dayTime = (int)strtotime($end_date)-(int)strtotime($begin_date) ;
              if (strtotime($end_date)-strtotime($begin_date) < 0) {
                  Cm::$res->error('开始时间要小于结束时间');
              }

              if (round($dayTime/(24*3600)) > 7) {
                  Cm::$res->error('时间跨度小于等于一个星期【7天】');
              }
          } else {
              Cm::$res->error('请传入结束时间');
          }
      }
      $wechat = new wxAnalysis();
      $wechat->analysisUser($appid, $begin_date, $end_date);
      $wechat->analysisMsg($appid, $begin_date, $end_date);
      $wechat->analysisArticle($appid, $begin_date, $end_date);

      Cm::$res->susess('同步成功！');
  }

  public static function syncAll($appid)
  {
      $wechat = new wxAnalysis();
      $wechat->analysisUser($appid);
      $wechat->analysisMsg($appid);
      $wechat->analysisArticle($appid);
      return true;
  }

  public function actionUser_report()
  {
      return (new AnalysisModel())->actionUserReport();
  }
  public function  actionUser_total()
  {
      return (new AnalysisModel())->actionUserTotal();
  }

  public function actionMessage_report()
  {
      return (new AnalysisModel())->actionMessageReport();
  }

  public function actionUser_home_report()
  {
      return (new AnalysisModel())->actionUseHomeReport();
  }

  public function actionUser_home_total()
  {
      return (new AnalysisModel())->actionUserHomeTotal();
  }



}